package com.example.mynews.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.example.mynews.Pojo.Pojo_banner;
import com.example.mynews.R;

import java.util.List;

//轮播图
public class News_pagerAdapter extends PagerAdapter {
    private  List<Pojo_banner.DataBean.TopnewsBean> list;
     private Context context;
    private ImageView[] img;
    public News_pagerAdapter(List<Pojo_banner.DataBean.TopnewsBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = LayoutInflater.from(context).inflate(R.layout.news_banner, container, false);
        ImageView imageView = view.findViewById(R.id.n_img);
        LinearLayout layout = view.findViewById(R.id.n_viewpager_t);
        TextView text = view.findViewById(R.id.n_viewpager_title);
        Glide.with(context).load("http://192.168.1.109:8080/web_home" +
                list.get(position).getTopimage()).into(imageView);
        text.setText(list.get(position).getTitle());
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

        img = new ImageView[list.size()];
        for (int i = 0; i < list.size(); i++) {
            ImageView imge = new ImageView(container.getContext());
            //设置布局参数
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(40, 40);
            params.setMargins(10, 0, 10, 0);
            imge.setLayoutParams(params);
            if (i == position) {
                imge.setBackgroundResource(R.mipmap.page_indicator_focused);
            } else {
                imge.setBackgroundResource(R.mipmap.page_indicator_unfocused);
            }
            img[i] = imageView;
            layout.addView(imge);
        }
            container.addView(view);
            return view;
        }
}
